package ldkxingzhe.ldkxingzhedemo.intent;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import ldkxingzhe.ldkxingzhedemo.service.NoticeIntentService;
import ldkxingzhe.ldkxingzhedemo.utils.LogDemo;

/**
 * Created by ke on 15-9-18.
 */
public class LocalBroadcastActivity extends AppCompatActivity implements View.OnClickListener {

    private final String TAG = LocalBroadcastActivity.class.getSimpleName();

    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FrameLayout layout = new FrameLayout(this);
        textView = new TextView(this);
        textView.setText("start ...");
        layout.addView(textView);
        setContentView(layout);
        layout.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter filter = new IntentFilter(NoticeIntentService.BROADCAST);
        LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver,filter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mBroadcastReceiver);
    }

    @Override
    public void onClick(View v) {
        LogDemo.v(TAG,"onClick");
        startService(new Intent(this,NoticeIntentService.class));
    }

    BroadcastReceiver mBroadcastReceiver = new  BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {
            LogDemo.v(TAG,"onReceive");
            textView.setText("End");
        }
    };
}
